package mysql

import (
	"beergo/lib/tool"
	"encoding/json"
	"fmt"
)

func CreateConfig(c *Creator) {
	data := twigger.Render("/mysql/dict.twig", c)
	path := fmt.Sprintf("%s/app/%s/conf/dict/%s.json", root, c.Table.Path, c.Table.Name)
	if !tool.FileExist(path) {
		tool.CreateFile(path, []byte(data))
	}
}

func CreateConfigPreload(creator *Creator) {
	path := fmt.Sprintf("%s/app/%s/conf/preload/%sPreload.go", root, creator.Table.Path, creator.Table.Name)
	tool.CreateFile(path, []byte(twigger.Render("/mysql/preload.twig", creator)))
}

func CreateOrmConfigFile(c *Creator) {
	path := fmt.Sprintf("%s/app/%s/conf/orm/%sConfig.json", root, c.Table.Path, c.Table.Name)
	data, err := json.MarshalIndent(c, " ", "  ")
	if err != nil {
		println("table json时发生错误")
	}
	tool.CreateFile(path, data)
}

func CreateModelConfigFile(c *Creator) {
	data := twigger.Render("/mysql/model_config.twig", c)
	path := fmt.Sprintf("%s/app/%s/conf/conf.go", root, c.Table.Path)
	if !tool.FileExist(path) {
		tool.CreateFile(path, []byte(data))
	}
}
